package ac.svn.accesscontrol.manager;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import ac.svn.accesscontrol.store.AccessUnit;
import ac.svn.accesscontrol.store.ISVNItem;

public class SVNInfoManagerHelper 
{
    private static final String DEFAULT = "Defualt";
    private static Map<String ,SVNInfoManager> infoManagerMap = new HashMap<String, SVNInfoManager>();

    static{
	infoManagerMap.put(DEFAULT, SVNInfoManager.getInfoManager());
    }

    /**
     * Method adds an information manager with specified name. Same name is returned back from this method
     * @param managerName
     * @param withCopiedValue
     * @return
     */
    public static String createInformationManager(String managerName,boolean withCopiedValue)
    {
	SVNInfoManager infoManager = SVNInfoManager.getNewSvnInfoManager(withCopiedValue);
	infoManagerMap.put(managerName, infoManager);		
	return managerName;
    }


    /***
     * Method returns instance of information Manager created earlier with specified name. 
     * @param managerName
     * @return
     */
    public static SVNInfoManager getInfoManager(String managerName)
    {
	return infoManagerMap.get(managerName);
    }

    /***
     * Returns instance of default information manager.
     * @return
     */
    public static SVNInfoManager getDefaultInfoManager()
    {
	return infoManagerMap.get(DEFAULT);
    }

    /***
     * Returns a list of names of all created information managers.
     * @return
     */
    public static Set<String> getInfoManagerList()
    {
	return infoManagerMap.keySet();
    }

    /***
     * Method is responsible for retrieving group or user associated with access unit.
     * @param accessUnitId
     * @param infoManagerId
     * @return
     */
    public static ISVNItem getOwnerOfAccessUnit(String accessUnitId,String infoManagerId)
    {
	ISVNItem svnItem = null;
	SVNInfoManager infoManager = null;
	if(null == infoManagerId)
	{
	    infoManager = infoManagerMap.get(DEFAULT);
	}
	else
	{
	    infoManager = infoManagerMap.get(infoManagerId);
	    if(null == infoManager)
	    {
		infoManager = infoManagerMap.get(DEFAULT);
	    }
	}

	AccessUnit item = infoManager.getAccessUnit(accessUnitId);
	if(null != item)
	{
	    svnItem = infoManager.getGroup(item.getOwnerId());
	    if(null == svnItem)
	    {
		svnItem = infoManager.getSvnUser(item.getOwnerId());
	    }
	}
	return svnItem;
    }

}
